package com.youyi.yyfilepickerlibrary.core.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.lkx.liandianqinew.R;
import com.youyi.yyfilepickerlibrary.core.SelectOptions;
import com.youyi.yyfilepickerlibrary.core.adapter.FragmentPagerAdapter;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import com.youyi.yyfilepickerlibrary.core.model.FileScanActEvent;
import com.youyi.yyfilepickerlibrary.core.model.FileScanFragEvent;
import com.youyi.yyfilepickerlibrary.core.model.FileScanSortChangedEvent;
import com.youyi.yyfilepickerlibrary.core.util.Const;
import java.util.ArrayList;
import java.util.Arrays;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class SelectFileByScanActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private boolean mCanPreview = true;
    private MenuItem mCountMenuItem;
    private int mSelectSortTypeIndex = 0;
    private ArrayList<EssFile> mSelectedFileList = new ArrayList<>();
    private TabLayout mTabLayout;
    private Toolbar mToolBar;
    private ViewPager mViewPager;

    private void initData() {
    }

    @Override // android.support.v4.view.ViewPager.OnPageChangeListener
    public void onPageScrollStateChanged(int i) {
    }

    @Override // android.support.v4.view.ViewPager.OnPageChangeListener
    public void onPageScrolled(int i, float f, int i2) {
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.SupportActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_select_file_by_scan);
        EventBus.getDefault().register(this);
        initUi();
        initData();
    }

    private void initUi() {
        this.mViewPager = (ViewPager) findViewById(R.id.vp_select_file_scan);
        this.mTabLayout = (TabLayout) findViewById(R.id.tabl_select_file_scan);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        this.mToolBar = toolbar;
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("文件选择");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        this.mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByScanActivity.AnonymousClass1 */

            public void onClick(View view) {
                SelectFileByScanActivity.this.onBackPressed();
            }
        });
        this.mTabLayout.setTabGravity(1);
        this.mTabLayout.setTabMode(0);
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < SelectOptions.getInstance().getFileTypes().length; i++) {
            arrayList.add(FileTypeListFragment.newInstance(SelectOptions.getInstance().getFileTypes()[i], SelectOptions.getInstance().isSingle, SelectOptions.getInstance().maxCount, SelectOptions.getInstance().getSortType(), i + 3));
        }
        this.mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), arrayList, Arrays.asList(SelectOptions.getInstance().getFileTypes())));
        this.mTabLayout.setupWithViewPager(this.mViewPager);
        this.mViewPager.setOffscreenPageLimit(arrayList.size() - 1);
        this.mViewPager.addOnPageChangeListener(this);
    }

    @Subscribe
    public void onFragSelectFile(FileScanFragEvent fileScanFragEvent) {
        if (!fileScanFragEvent.isAdd()) {
            this.mSelectedFileList.remove(fileScanFragEvent.getSelectedFile());
        } else if (SelectOptions.getInstance().isSingle) {
            this.mSelectedFileList.add(fileScanFragEvent.getSelectedFile());
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, this.mSelectedFileList);
            setResult(-1, intent);
            super.onBackPressed();
            return;
        } else {
            this.mSelectedFileList.add(fileScanFragEvent.getSelectedFile());
        }
        this.mCountMenuItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(SelectOptions.getInstance().maxCount)));
        EventBus.getDefault().post(new FileScanActEvent(SelectOptions.getInstance().maxCount - this.mSelectedFileList.size()));
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.browse_menu, menu);
        MenuItem findItem = menu.findItem(R.id.browser_select_count);
        this.mCountMenuItem = findItem;
        findItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(SelectOptions.getInstance().maxCount)));
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        if (itemId == R.id.browser_select_count) {
            if (this.mSelectedFileList.isEmpty()) {
                return true;
            }
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, this.mSelectedFileList);
            setResult(-1, intent);
            super.onBackPressed();
        } else if (itemId == R.id.browser_sort) {
            new AlertDialog.Builder(this).setSingleChoiceItems(R.array.sort_list_scan, 0, new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByScanActivity.AnonymousClass4 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    SelectFileByScanActivity.this.mSelectSortTypeIndex = i;
                }
            }).setNegativeButton("降序", new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByScanActivity.AnonymousClass3 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    int i2 = SelectFileByScanActivity.this.mSelectSortTypeIndex;
                    if (i2 == 0) {
                        SelectOptions.getInstance().setSortType(1);
                    } else if (i2 == 1) {
                        SelectOptions.getInstance().setSortType(2);
                    } else if (i2 == 2) {
                        SelectOptions.getInstance().setSortType(5);
                    }
                    EventBus.getDefault().post(new FileScanSortChangedEvent(SelectOptions.getInstance().getSortType(), SelectFileByScanActivity.this.mViewPager.getCurrentItem()));
                }
            }).setPositiveButton("升序", new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByScanActivity.AnonymousClass2 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    int i2 = SelectFileByScanActivity.this.mSelectSortTypeIndex;
                    if (i2 == 0) {
                        SelectOptions.getInstance().setSortType(0);
                    } else if (i2 == 1) {
                        SelectOptions.getInstance().setSortType(3);
                    } else if (i2 == 2) {
                        SelectOptions.getInstance().setSortType(4);
                    }
                    EventBus.getDefault().post(new FileScanSortChangedEvent(SelectOptions.getInstance().getSortType(), SelectFileByScanActivity.this.mViewPager.getCurrentItem()));
                }
            }).setTitle("请选择").show();
        }
        return true;
    }

    @Override // android.support.v4.view.ViewPager.OnPageChangeListener
    public void onPageSelected(int i) {
        EventBus.getDefault().post(new FileScanActEvent(SelectOptions.getInstance().maxCount - this.mSelectedFileList.size()));
    }
}
